package com.cody.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cody.dao.AnswerDao;
import com.cody.entity.Answer;
import com.cody.entity.Student;
import com.cody.entity.Teacher;
import com.cody.service.StudentService;
import com.cody.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Cody
 * @date 2021/4/6 - 20:38
 */
@Controller
public class IndexController {

    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;

    @RequestMapping("/demo")
    public String demo() {
        return "demo";
    }
    @RequestMapping("/")
    public String login() {
        return "login";
    }


    @RequestMapping("/login")
    public String login(@RequestParam(required = false,name = "username") String username, @RequestParam(required = false,name = "password") String password
            , @RequestParam(required = false,name = "procession") String procession, HttpSession session
    ) {
        JSONObject jsonObject;
        //jsonObject = JSONObject.parseObject(JSON.toJSONString(student));

        //选择框：
        if ("学生".equals(procession)){
            Student student;
            student = studentService.querySingleStudent(username, password);
            session.setAttribute("user", student);
            session.setAttribute("userType",procession);
            if (student == null) {
                //用户名或者密码错误
                session.setAttribute("msg", "此用户类型下查无此人");
                return "login";
            }

            return "redirect:/student/index";
        } else if("老师".equals(procession)){
            Teacher teacher;
            teacher = teacherService.querySingleTeacher(username, password);
            session.setAttribute("userType",procession);
            session.setAttribute("user", teacher);
            if (teacher == null) {
                //用户名或者密码错误
                session.setAttribute("msg", "此用户类型下查无此人");
                return "login";
            }
            return "redirect:/teacher/index";
        }
        else{
            session.setAttribute("msg", "请登录");
            return "login";
        }
    }
    @RequestMapping("/register")
    public String register(@RequestParam(required = false,name = "userNumber") String username,
                           @RequestParam(required = false,name = "username") String userNumber,
                           @RequestParam(required = false,name = "password") String password,
                           @RequestParam(required = false,name = "rePassword") String rePassword,
                           @RequestParam(required = false,name = "sex") Integer sex,
                           @RequestParam(required = false,name = "procession") String procession, HttpSession session
    ) {

        if(!password.equals(rePassword)){
          session.setAttribute("msg","两次密码输入不一致！");
            return "register";
        }

        else{
            //选择框：
            if ("学生".equals(procession)){
                //可能null 爆空指针
                Student student = new Student(1,username,userNumber,sex,password,null,null,null);
                studentService.insertSingleStudent(student);
                return "redirect:login";
            } else if("老师".equals(procession)){
                Teacher teacher = new Teacher(1,null,username,userNumber,sex,password,null,null,null,null);
                teacherService.insertSingleTeacher(teacher);
                return "redirect:login";
            }
        }
        return "login";

    }

    @RequestMapping("/test01")
    public ModelAndView test01(ModelAndView modelAndView){
        modelAndView.setViewName("/login");
        return modelAndView;
    }

    @Autowired
    private AnswerDao answerDao;
    @RequestMapping("/test02")
    @ResponseBody
    public List test02(){
        List answers = answerDao.test();
        return answers;
    }
}